package server.business.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import server.business.mapper.BloodOrderMapper;
import server.business.mapper.OrderBloodReviewMapper;
import server.business.mapper.OrderBloodStateMapper;
import server.business.pojo.*;
import server.business.service.BloodOrderService;
import server.business.service.OrderBloodReviewService;
import server.business.utils.UUIDUtils;
import server.business.vo.Result;
import server.business.vo.ResultExpand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/bloodOrder")
public class BloodOrderController {
    
    @Autowired
    private BloodOrderService bloodOrderService;
    
//    @RequestMapping("/bloodOrders")
//    public String toOrderBlood(){
//        return "/pages/bloodOrders";
//    }
    
    @RequestMapping("/normal/query")
    @ResponseBody
    public List<BloodOrderData> queryData(){
        List<BloodOrderData> bloodOrderData = bloodOrderService.queryData();
        return bloodOrderData;
    }

    /**
     * 分页查询
     *
     * @param request  请求
     * @param response 响应
     * @return {@link ResultExpand}
     */
    @RequestMapping("/normal/listByPage")
    @ResponseBody
    public ResultExpand listByPage(HttpServletRequest request, HttpServletResponse response){
        ResultExpand resultExpand = new ResultExpand();
        resultExpand = bloodOrderService.listByPage(request, response);
        return resultExpand;
    }

    /**
     * 搜索血液订单
     *
     * @param request  请求
     * @param response 响应
     * @return {@link ResultExpand}
     */
    @RequestMapping("/normal/searchBloodOrder")
    @ResponseBody
    public ResultExpand searchBloodOrder(HttpServletRequest request, HttpServletResponse response){
        ResultExpand resultExpand = new ResultExpand();
        resultExpand = bloodOrderService.searchBloodOrder(request, response);
        return resultExpand;
    }


    /**
     * 血液修改订单
     *
     * @param bloodOrderData 血订单数据
     * @return {@link ResultExpand}
     */
    @RequestMapping("/normal/modifyBloodOrder")
    @ResponseBody
    public Result modifyBloodOrder(@RequestBody BloodOrderData bloodOrderData){
        Result result = new Result();
        result = bloodOrderService.modifyBloodOrder(bloodOrderData);
        return result;
    }

    /**
     * 驳回血液订单
     *
     * @param bloodOrderData 血液订单数据
     * @return {@link Result}
     */
    @RequestMapping("/review/refuseBloodOrder")
    @ResponseBody
    public Result refuseBloodOrder(@RequestBody BloodOrderData bloodOrderData){
        Result result = new Result();
        result = bloodOrderService.refuseBloodOrder(bloodOrderData);
        return result;
    }

    /**
     * 通过血液订单
     *
     * @param bloodOrderData 血订单数据
     * @return {@link Result}
     */
    @RequestMapping("/review/passBloodOrder")
    @ResponseBody
    public Result passBloodOrder(@RequestBody BloodOrderData bloodOrderData){
        Result result = new Result();
        result = bloodOrderService.passBloodOrder(bloodOrderData);
        return result;
    }

    /**
     * 提交订单
     *
     * @param bloodOrderList  血订单列表
     * @param applicant       申请人
     * @param applicantNumber 申请人数量
     * @param applyCompany    适用于公司
     * @return {@link Result}
     */
    @RequestMapping(value = "/normal/submitOrder", method = RequestMethod.POST)
    @ResponseBody
    public Result submitOrder(@RequestBody BloodOrderData[] bloodOrderList, @RequestParam(name = "applicant") String applicant, @RequestParam(name = "applicantNumber") String applicantNumber, @RequestParam(name = "applyCompany") String applyCompany){
        Result result = new Result();
        result = bloodOrderService.submitOrder(bloodOrderList, applicant, applicantNumber, applyCompany);
        return result;
    }
    
    @RequestMapping(value = "/normal/deleteBloodOrder", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteBloodOrder(@RequestBody BloodOrderData bloodOrderData) {
        Result result = new Result();
        result = bloodOrderService.deleteBloodOrder(bloodOrderData);
        return result;
    }
    
    /**
     * 下载excel
     *
     * @param request  请求
     * @param response 响应
     */
    @RequestMapping("/normal/downloadExcel")
    public void downloadExcel(HttpServletRequest request, HttpServletResponse response){
        bloodOrderService.downloadExcel(request, response);
    }
}
